package com.template.server.controller;

import com.template.server.common.exception.BizException;
import com.template.server.common.result.R;
import com.template.server.entity.Example;
import com.template.server.service.ExampleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author liuxf
 * @date 2024/4/11
 */
@Api(tags = "例子管理")
@RestController
@RequestMapping("/example")
public class ExampleController {

    @Autowired
    private ExampleService exampleService;

    @ApiOperation("例子列表")
    @GetMapping(value = "/test")
    public R test() {
        int a = 1 / 0;
        throw new BizException("不准操作");
        // return R.ok(exampleService.list());
    }

    @ApiOperation("例子列表")
    @GetMapping(value = "/list")
    public R list() {
        return R.ok(exampleService.list());
    }

    @ApiOperation("获取例子")
    @GetMapping(value = "/get/{id}")
    public R get(@PathVariable("id") Integer id) {
        return R.ok(exampleService.getById(id));
    }

    @ApiOperation("新增例子")
    @PostMapping(value = "/add")
    public R add(@RequestBody Example example) {
        return R.ok(exampleService.save(example));
    }

    @ApiOperation("修改例子")
    @PutMapping(value = "/modify/{id}")
    public R modify(@PathVariable("id") Integer id, @RequestBody Example example) {
        example.setId(id);
        return R.ok(exampleService.updateById(example));
    }

    @ApiOperation("删除例子")
    @DeleteMapping(value = "/remove/{id}")
    public R remove(@PathVariable("id") Integer id) {
        return R.ok(exampleService.removeById(id));
    }

}
